from sys import exit
from sys import argv
import os

# 设置标识，表明是显示还是>
copy_flag = False
# argv = ["ex05.py", "22", ">", "33"]

# 判断输入参数，小于2，则报错
if len(argv) < 2:
    print("please input file name !")
    # 退出程序
    exit()

for arg in argv:
    # 判断是否有>
    if arg == ">":
        copy_flag = True
        break
    # 判断文件是否存在，不存在则报错退出程序
    elif not os.path.exists(arg):
        print("please input right file name.")
        exit()

if copy_flag:
    # 拷贝模式
    # 将新文件弹出堆栈
    out_file_name = argv.pop()
    # 将>弹出
    argv.pop()
    with open(out_file_name, 'w+') as out_file:
        for i in range(1, len(argv)):
            input_file = argv[i]
            with open(input_file) as input:
                out_file.write(input.read())
        # print(out_file.read())
    # pass
else:
    # 显示模式
    for i in range(1, len(argv)):
        input_file = argv[i]
        with open(input_file) as file:
            print(file.read())
